package cho114366.basic;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.spi.DirStateFactory.Result;

public class J19_ex0801 {

	private static final String DRV = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@//localhost:1521/xe";
	private static final String USR = "scott";
	private static final String PWD = "TIGER";

	
	private static final String createSQL = " create table test(cd_id char(5),title varchar(50), publisher varchar(30), artise varchar(20), price number(10) ";
	private static final String insertSQL = " insert into test(cd_id,title,publisher,artist,price) values(?,?,?,?,?) ";
	private static final String selectSQL = " select * from test ";
	public static void main(String[] args) {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String result = "";
		try {
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(createSQL);
			int cnt = ps.executeUpdate();
			System.out.println(cnt + 1 + "건의 데이터입력");
			ps.close();
			ps = conn.prepareStatement(insertSQL);
			int cnt1 = ps.executeUpdate();
			System.out.println(cnt1 + "건의 데이터입력");
			ps.close();
			ps= conn.prepareStatement(selectSQL);
			rs=ps.executeQuery();
			while (rs.next()) {
				for (int i = 1; i <= 5; ++i)
					result += rs.getString(i);
				result += "\n";

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {

					e.printStackTrace();
				}

		}

	}
}
